package com.myzl.coal.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import com.myzl.coal.domain.Device;
import com.myzl.coal.mongo.domain.HistoryData;
import com.myzl.coal.service.IMongoHistoryDataService;
import com.myzl.coal.service.MongoHistoryDataMapper;
import com.myzl.common.enums.DeviceCommandEnum;
import com.myzl.common.enums.DeviceTypeEnum;
import com.myzl.common.enums.PictureRecognitionEnum;
import com.myzl.common.enums.SensorEnum;
import com.myzl.common.utils.SpecificDateUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName: HistoryDataImpl
 * @Author: wzb
 * @Date: 2023/6/12 10:00
 * @Description:
 */
@Slf4j(topic = "admin-user")
@Service
@Data
public class MongoHistoryDataServiceImpl implements IMongoHistoryDataService {
    private final MongoHistoryDataMapper historyDataMapper;

    @Override
    public Map<String,List<String>> thirtyCoalProportion(Integer deviceId) {
        DateTime createTime = DateUtil.offsetMinute(new Date(), -30);
        List<Integer> params = Arrays.asList(PictureRecognitionEnum.LUMP_COAL.getCode(), PictureRecognitionEnum.SIEVE.getCode(), PictureRecognitionEnum.SLACK.getCode());
        List<HistoryData> historyData = historyDataMapper.selectList(deviceId, params, createTime);

        List<HistoryData> collect = historyData.stream().filter(f -> f.getDictValue() == PictureRecognitionEnum.LUMP_COAL.getCode()).sorted(Comparator.comparing(HistoryData::getCreateTime)).collect(Collectors.toList());
        List<String> createTimes = collect.stream().map(m-> DateUtil.format(m.getCreateTime(), "HH:mm")).collect(Collectors.toList());
        List<String> lump_coal = collect.stream().map(HistoryData::getDataValue).collect(Collectors.toList());
        List<String> sieve = historyData.stream().filter(f->f.getDictValue()==PictureRecognitionEnum.SIEVE.getCode()).sorted(Comparator.comparing(HistoryData::getCreateTime)).map(HistoryData::getDataValue).collect(Collectors.toList());
        List<String> slack = historyData.stream().filter(f->f.getDictValue()==PictureRecognitionEnum.SLACK.getCode()).sorted(Comparator.comparing(HistoryData::getCreateTime)).map(HistoryData::getDataValue).collect(Collectors.toList());

        Map<String,List<String>> map=new HashMap<>();
        map.put("createTimes",createTimes);
        map.put("lump_coal",lump_coal);
        map.put("sieve",sieve);
        map.put("slack",slack);
        return map;
    }

    @Override
    public Map<String, List<String>> thirtyPowerTrend(Integer deviceId) {
        DateTime createTime = DateUtil.offsetMinute(new Date(), -30);
        List<Integer> params = Collections.singletonList(SensorEnum.POWER.getAddress());
        List<HistoryData> dataList = historyDataMapper.selectList(deviceId, params, createTime);
        List<String> timeList = dataList.stream()
                .filter(f -> f.getDictValue() == SensorEnum.POWER.getAddress())
                .sorted(Comparator.comparing(HistoryData::getCreateTime))
                .collect(Collectors.toList())
                .stream().map(m-> DateUtil.format(m.getCreateTime(), "HH:mm")).collect(Collectors.toList());

        List<String> powerList = dataList.stream().filter(f->f.getDictValue()==SensorEnum.POWER.getAddress()).sorted(Comparator.comparing(HistoryData::getCreateTime)).map(HistoryData::getDataValue).collect(Collectors.toList());

        Map<String,List<String>> map=new HashMap<>();
        map.put("xData",timeList);
        map.put("yData",powerList);
        return map;
    }

    @Override
    public Map<String, List<String>> thirtyTemperatureTrend(Integer deviceId) {
        DateTime createTime = DateUtil.offsetMinute(new Date(), -30);
        List<Integer> params = Stream.of(SensorEnum.MOTOR_TEMPERATURE.getAddress(), SensorEnum.EXCITER_TEMPERATURE.getAddress()).collect(Collectors.toList());
        List<HistoryData> dataList = historyDataMapper.selectList(deviceId, params, createTime);
        List<String> timeList = dataList.stream()
                .filter(f -> f.getDictValue() == SensorEnum.MOTOR_TEMPERATURE.getAddress())
                .sorted(Comparator.comparing(HistoryData::getCreateTime))
                .collect(Collectors.toList())
                .stream().map(m-> DateUtil.format(m.getCreateTime(), "HH:mm")).collect(Collectors.toList());

        List<String> motorTemperatureList = dataList.stream().filter(f->f.getDictValue()==SensorEnum.MOTOR_TEMPERATURE.getAddress()).sorted(Comparator.comparing(HistoryData::getCreateTime)).map(HistoryData::getDataValue).collect(Collectors.toList());
        List<String> exciterTemperatureList = dataList.stream().filter(f->f.getDictValue()==SensorEnum.EXCITER_TEMPERATURE.getAddress()).sorted(Comparator.comparing(HistoryData::getCreateTime)).map(HistoryData::getDataValue).collect(Collectors.toList());

        Map<String,List<String>> map=new HashMap<>();
        map.put("xData",timeList);
        map.put("yData1",motorTemperatureList);
        map.put("yData2",exciterTemperatureList);
        return map;
    }

    @Override
    public boolean clearOtherData() {
        //本月第一条
        DateTime currentMonth = SpecificDateUtil.getMsCurrentMonth(DateUtil.today());
        return historyDataMapper.deleteList(DeviceTypeEnum.DOUBLE_ORDER.getCode(),currentMonth);
    }

    @Override
    public boolean clearScaleData(Device device) {
        List<String> idList = new ArrayList<>();
        // 获取所有参数
        for (DeviceCommandEnum value : DeviceCommandEnum.values()) {
            // 查询没一个参数的历史第一条
            HistoryData allHistoryData = historyDataMapper.selectOne(device.getId(), value.getCode());
            if (ObjUtil.isNotEmpty(allHistoryData)){
                idList.add(allHistoryData.getId());
            }
            //今年第一条
            /*DateTime currentYear = SpecificDateUtil.getMsCurrentYear(DateUtil.today());
            HistoryData yearHistoryData = historyDataMapper.selectOne(device.getId(),value.getCode(), currentYear);
            if (ObjUtil.isNotEmpty(yearHistoryData)){
                idList.add(yearHistoryData.getId());
            }*/
            //本月第一条
            /*DateTime currentMonth = SpecificDateUtil.getMsCurrentMonth(DateUtil.today());
            HistoryData monthHistoryData = historyDataMapper.selectOne(device.getId(),value.getCode(), currentMonth);
            if (ObjUtil.isNotEmpty(monthHistoryData)){
                idList.add(monthHistoryData.getId());
            }*/
        }
        //  一年内数据
        DateTime agoTwoYear = SpecificDateUtil.getAgoOneYear(DateUtil.today());
        return historyDataMapper.deleteList(device.getId(), idList, agoTwoYear);
    }
}
